﻿using UnityEngine;
using System.Collections;
using Dict;
using Dict.Config;
using System.Collections.Generic;
using System;
using ProtoBuf.Message;
namespace UI
{
    public class UIDropSimpleParser
    {
        public class UISimpleItem
        {
            public string id;
            public int number;
            public UISimpleItem( string id, int number )
            {
                this.id = id;
                this.number = number;
            }

            public bool Goods
            {
                get
                {
                    return id.Contains( "_" );
                }
            }
        }

        public List<UISimpleItem> items = new List<UISimpleItem>();

        public UISimpleItem Get( string id )
        {
            foreach( var it in items )
            {
                if( id == it.id ) return it;
            }

            throw new Exception( "can't find this item:id ="+id );
        }
        public int Coin
        {
            get
            {
                int ret =0;
                foreach( var it in items )
                {
                    if( it.id == TableConst.coin )
                    {
                        return it.number;
                    }
                }

                return ret;
            }
        }

        public int Exp
        {
            get
            {
                foreach( var it in items )
                {
                    if( it.id == "exp" )
                    {
                        return it.number;
                    }
                }

                return 0;
            }
        }


        static public string Icon( string id, out TextureIcon iconType, out bool is_frag )
        {
            if( id.Contains( "_" ) )
            {
                var pos  = id.LastIndexOf( "_" );
                string prefix = id.Substring( 0, pos );
                string icon="";
                is_frag=false;
                iconType = TextureIcon.ITEM;

                Debug.Log( "prefix:"+prefix );

                if( prefix == TableConst.war )
                {
                    icon = DictDataManager.Instance.tableHeroDao.GetById( id ).iconName;
                    iconType = TextureIcon.HERO;
                }
                else if( prefix == TableConst.trea )
                {
                    icon = DictDataManager.Instance.tableTreasureDao.GetById( id ).icon;
                    iconType = TextureIcon.TREASURE;
                }
                else if( prefix == TableConst.item )
                {
                    icon = DictDataManager.Instance.tableItemDao.GetById( id ).icon;
                    iconType = TextureIcon.ITEM;
                }
                else if( prefix == TableConst.equip )
                {
                    icon = DictDataManager.Instance.tableEquipDao.GetById( id ).icon;
                    iconType = TextureIcon.EQUIP;
                }

                else if( prefix == TableConst.frag_equip )
                {
                    is_frag=true;
                    var target_id = DictDataManager.Instance.tableFragDao.GetById( id ).target;
                    icon = DictDataManager.Instance.tableEquipDao.GetById( target_id ).icon;
                    iconType = TextureIcon.EQUIP;
                }

                else if( prefix ==TableConst.frag_trea )
                {
                    is_frag=true;
                    var target_id = DictDataManager.Instance.tableFragDao.GetById( id ).target;
                    icon = DictDataManager.Instance.tableTreasureDao.GetById( target_id ).icon;
                    iconType = TextureIcon.TREASURE;
                }

                else if( prefix == TableConst.frag_war )
                {
                    is_frag=true;
                    var target_id = DictDataManager.Instance.tableFragDao.GetById( id ).target;
                    icon = DictDataManager.Instance.tableHeroDao.GetById( target_id ).iconName;
                    iconType = TextureIcon.HERO;
                }
                return icon;
            }

            is_frag=false;
            iconType = TextureIcon.ITEM;
            return null;
        }

        public UIDropSimpleParser( ResultObject result )
        {
            items.Clear();

            foreach( var it in result.simps )
            {
                bool has = false;
                foreach( var obj in items )
                {
                    if( obj.id == it.id )
                    {
                        has =true;
                        obj.number+=it.num;
                        break;
                    }
                }

                if( !has )
                {
                    items.Add( new UISimpleItem( it.id, it.num ) );
                }
            }

            foreach( var it in result.wars )
            {
                bool has = false;
                foreach( var obj in items )
                {
                    if( obj.id == it.id )
                    {
                        has =true;
                        obj.number+=1;
                        break;
                    }
                }

                if( !has )
                {
                    items.Add( new UISimpleItem( it.id, 1 ) );
                }
            }

            foreach( var it in result.treas )
            {
                bool has = false;
                foreach( var obj in items )
                {
                    if( obj.id == it.treasureId )
                    {
                        has =true;
                        obj.number+=1;
                        break;
                    }
                }

                if( !has )
                {
                    items.Add( new UISimpleItem( it.treasureId, 1 ) );
                }
            }

            foreach( var it in result.equips )
            {
                bool has = false;
                foreach( var obj in items )
                {
                    if( obj.id == it.equipId )
                    {
                        has =true;
                        obj.number+=1;
                        break;
                    }
                }

                if( !has )
                {
                    items.Add( new UISimpleItem( it.equipId, 1 ) );
                }
            }

        }
    }
}